<template>
  <a-button class="left" :loading="uploading" @click="exportExcel" type="primary" ghost>导出数据</a-button>
</template>
<script>
import XLSX from 'xlsx'
import moment from 'moment'
import { getMoneyarrange } from '@/api/common'
import { LoanState } from '@/dict'
export default {
  name: 'fundExport',
  props: ['waitColumns', 'finishColumns'],
  data: () => ({
    uploading: false
  }),
  methods: {
    async exportExcel() {
      this.uploading = true
      /* 从表生成工作簿对象 */
      const res = await getMoneyarrange()

      const wait = res.result?.data?.waitlist || []
      const finish = res.result?.data?.finishlist || []
      const waitExportData = wait.map((el, i) => {
        el.remittanceDate = el.remittanceDate ? moment(el.remittanceDate * 1000).format('YYYY-MM-DD') : '-'
        el.approve = LoanState.find(e => e.value == el.approve)?.text
        el.index = i
        const res = {}
        this.waitColumns.forEach(col => {
          if (el[col.dataIndex]) {
            res[col.title] = el[col.dataIndex]
          }
        })
        return res
      })
      const finishExportData = finish.map((el, i) => {
        el.remittanceDate = el.remittanceDate ? moment(el.remittanceDate * 1000).format('YYYY-MM-DD') : '-'
        el.approve = LoanState.find(e => e.value == el.approve)?.text
        el.index = i
        const res = {}
        this.finishColumns.forEach(col => {
          if (el[col.dataIndex]) {
            res[col.title] = el[col.dataIndex]
          }
        })
        return res
      })
      // console.log(exportData)

      var ws = XLSX.utils.json_to_sheet(waitExportData)
      var wf = XLSX.utils.json_to_sheet(finishExportData)
      /* 获取二进制字符串作为输出 */
      var wb = XLSX.utils.book_new() /*新建book*/
      XLSX.utils.book_append_sheet(wb, ws, '待处理资金安排列表') /* 生成xlsx文件(book,sheet数据,sheet命名) */
      XLSX.utils.book_append_sheet(wb, wf, '已处理资金安排列表') /* 生成xlsx文件(book,sheet数据,sheet命名) */
      XLSX.writeFile(wb, '提款资金安排列表.xlsx') /*写文件(book,xlsx文件名称)*/
      this.uploading = false
    }
  }
}
</script>
<style lang="css" scoped>
.left {
  margin-left: 15px;
}
</style>
